import Vue from 'vue'
import moment from 'moment'
let toChinesNum = (num) => {
  let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
   let unit = ["", "十", "百", "千", "万"];
   num = parseInt(num);
   let getWan = (temp) => {
   　　let strArr = temp.toString().split("").reverse();
   　　let newNum = "";
   　　for (var i = 0; i < strArr.length; i++) {
     　　newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
  　　}
   　 return newNum;
 }
 let overWan = Math.floor(num / 10000);
 let noWan = num % 10000;
 if (noWan.toString().length < 4) {
　　　　　　noWan = "0" + noWan;
　　　 }
 return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
}
let thousandSep = (params) => {
    if (params === null || params === undefined || params==='') {
        return '--'
    } else if (isNaN(params)) {
        return 'NaN'
    } else {
        let num = params.toString()
        if (/^-?\d+\.?\d+$/.test(num)) {
            if (/^-?\d+$/.test(num)) {
                num = num.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
            } else {
                let numarray = num.split('.')
                let numbefore = numarray[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
                num = numbefore + '.' + numarray[1]
            }
        }
        return num
    }
}
Vue.filter('thousandSep',function(value){
    return thousandSep(value)

})
//数字转汉字
Vue.filter('toChinese',function(value){
  return toChinesNum(value)
}
)
Vue.filter('formatDate', function (value, formatString) {
  formatString = formatString || 'YYYY-MM-DD HH:mm:ss'
  if (value) {
    return moment(value).format(formatString)
  } else {
    return '--'
  }
})

let cityTypeOptions = {
  '1': '国家',
  '2': '省',
  '3': '市',
  '4': '区/县'
}
Vue.filter('cityTypeFilter', n => {
  if (!n) return '--'
  return cityTypeOptions[n]
})
let purposeOptions = {
  '1': '计量',
  '2': '结算',
  '3': '取样'
}
Vue.filter('purposeFilter', n => {
  if (!n) return '--'
  return purposeOptions[n]
})
Vue.filter('noValue', n => {
  if (!n && n !== 0) return '--';
  return n
});
Vue.filter('ellipsis', (str,length) => {
  str = str?str.toString():'';
  if(str.length>length){
    return str.slice(0, length) + "...";
  }else{
    return str;
  }
});
//千分位过滤器
Vue.filter('NumFormat', function (value) {

if (!value) return '0'

var intPart = Number(value).toFixed(0) // 获取整数部分

var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断

return intPartFormat

}

)
